<?php if (!defined('BASEPATH'))  exit('No direct script access allowed');


/**
 *  后台跳转
 * @access	public
 * @return	integer
 */
if (!function_exists('cpmessage')) {

    function cpmessage($message, $url_forward = '') {
        header("Content-type: text/html; charset=utf-8");
        if (!empty($url_forward)) {
            $message .= "<script>setTimeout(\"window.location.href ='$url_forward';\",1000);</script>";
        }
        echo '<style>.message {background: none repeat scroll 0 0 #f60; height: 42px;left: 50%; margin: -130px 0 0 -200px; position: fixed;top: 50%;width: 400px;}
            .message_tit {background: none repeat scroll 0 0 #2b579a;border-bottom: 2px solid #19478a;color: #fff; font-size: 20px;font-weight: bold;height: 42px; line-height: 42px;text-align: center;}
            </style><div class="message"><div class="message_tit">' . $message . '</div></div>';
        exit();
    }
}


/**
 *   反引用一个引用字符串
 *  控制器层过滤函数
 */
if( !function_exists('stripslashes_deep')){
    function stripslashes_deep($string)
    {
        return is_array($string) ? array_map('stripslashes_deep', $string) : stripslashes($string);
    }
}


/**
 *   使用反斜线引用字符串
 *   model层过滤函数
 */
if( !function_exists('addslashes_deep')){
    function addslashes_deep($string)
    {
        return is_array($string) ? array_map('addslashes_deep', $string) : addslashes($string);
    }
}

/**
 *  分页页码
 *
 * @access	public
 * @return	integer
 */
if (!function_exists('multi')) {

    function multi($num, $perpage, $curpage, $mpurl) {
        $page = 5;
        $multipage = '';
        $mpurl .= strpos($mpurl, '?') !== false ? '&' : '?';
        $realpages = 1;
        if ($num > $perpage) {
            $offset = 2;
            $realpages = @ceil($num / $perpage);
            $pages = $realpages;
            if ($page > $pages) {
                $from = 1;
                $to = $pages;
            } else {
                $from = $curpage - $offset;
                $to = $from + $page - 1;
                if ($from < 1) {
                    $to = $curpage + 1 - $from;
                    $from = 1;
                    if ($to - $from < $page) {
                        $to = $page;
                    }
                } elseif ($to > $pages) {
                    $from = $pages - $page + 1;
                    $to = $pages;
                }
            }
            $multipage = '';
            //$urlplus = $todiv?'#$todiv':'';
            $urlplus = '';
            if ($curpage - $offset > 1 && $pages > $page) {
                $multipage .= "<a ";
                $multipage .= "href=\"{$mpurl}page=1{$urlplus}\"";
                $multipage .= " class=\"first\">首页</a>";
            }
            if ($curpage > 1) {
                $multipage .= "<a ";
                $multipage .= "href=\"{$mpurl}page=" . ($curpage - 1) . "$urlplus\"";
                $multipage .= " class=\"prev\">&lsaquo;&lsaquo;上一页</a>";
            }
            for ($i = $from; $i <= $to; $i++) {
                if ($i == $curpage) {
                    $multipage .= '<a  class="hov">' . $i . '</a>';
                } else {
                    $multipage .= "<a ";
                    $multipage .= "href=\"{$mpurl}page=$i{$urlplus}\"";
                    $multipage .= ">$i</a>";
                }
            }
            if ($curpage < $pages) {
                $multipage .= "<a ";
                $multipage .= "href=\"{$mpurl}page=" . ($curpage + 1) . "{$urlplus}\"";
                $multipage .= " class=\"next\">下一页&rsaquo;&rsaquo;</a>";
            }
            if ($to < $pages) {
                $multipage .= "<a ";
                $multipage .= "href=\"{$mpurl}page=$pages{$urlplus}\"";
                //$multipage .= " class=\"last\">... $realpages</a>";
                $multipage .= " class=\"last\">末页</a>";
            }
            if ($multipage) {
                $multipage = '<a>&nbsp;共' . $num . '个&nbsp;</a>' . $multipage;
            }
        }
        return $multipage;
    }

}


// ------------------------------------------------------------------------
/**
 * 前端替换分页页码
 * author:yuchong
 * $page   当前页数
 * $pagecount  分页总数
 * $url 链接
 * @access	public
 * @return	integer
 */
if (!function_exists('frontmultiurl')) {
    function frontmultiurl($page, $pagecount, $url) {
        $html = '';
        if ($pagecount == 1) {
            return "";
        }
        //前缀
        if($page != 1 && $page < ($pagecount - 2))
        {
            $html .= '<a href="'. str_replace("page_mode", ($page-1), $url) .'" class="page_previous">上一页</a>';
        }
        else if($page != 1)
        {
            $html .= '<a href="'.str_replace("&page=page_mode", '', $url).'" class="fir_las">最前</a>';
            $html .= '<a href="'.str_replace("page_mode", ($page-1), $url).'" class="page_previous">上一页</a>';
        }
        $st = ($page < 3) ? 1 : ($page - 2);
        $ed = ($page > ($pagecount-3)) ? $pagecount : ($page + 2);
        //中间代码分隔
        for ($pnow = $st; $pnow <= $ed; $pnow++) {
            if ($pnow != $page) {
                $html .= '<a href="'.str_replace("page_mode", $pnow, $url).'" class="inactive">' . $pnow . '</a>';
            } else {
                $html .= '<span class="current">' . $pnow . '</span>';
            }
        }
        //后缀
        if ($page <= ($pagecount -2)) {
            $html .= '<a href="'.str_replace("page_mode", ($page+1), $url).'" class="page_next">下一页</a>';
            $html .= '<a href="'.str_replace("page_mode", $pagecount, $url).'" class="fir_las">最后</a>';
        }
        else if($page < $pagecount)
        {
            $html .= '<a href="'.str_replace("page_mode", ($page+1), $url).'" class="page_next">下一页</a>';
        }

        return $html;
    }
}


// ------------------------------------------------------------------------
/**
 *  ajax分页页码
 *
 * @access	public
 * @return	integer
 */
if (!function_exists('getPageNav')) {

    function getPageNav($page,$count,$size,$fun="showlist",$t=0)
    {
        if($count)
        {
            $pageCount=$count%$size==0?floor($count/$size):floor($count/$size)+1;
            $html='';
            if($page!=1)
            {
                $html.='<a href="javascript:'.$fun.'(1)">首页</a>';
                $html.='<a href="javascript:'.$fun.'('.($page-1).')")">上一页</a>';
            }
            else
            {
                $html.='<a href="javascript:'.$fun.'(1)">首页</a>';
                $html.='<a href="javascript:'.$fun.'(1)")">上一页</a>';
            }
            $st = $ed = 0;
            if($page-4<1)
            {
                $st=1;
            }
            else
            {
                $st=$page-4;
            }
            if($page+4>$pageCount)
            {
                $ed=$pageCount;
            }
            else
            {
                $ed=$page+4;
            }
            for($plist=$st;$plist<=$ed;$plist++)
            {
                if($plist!=$page)
                {
                    $html.='<a href="javascript:'.$fun.'('.$plist.')">'.$plist.'</a>';
                }
                else
                {
                    $html.='<a class="p_hov">'.$plist.'</a>';
                }
            }
            if($page!=$pageCount)
            {
                $html.='<a href="javascript:'.$fun.'('.($page+1).')">下一页</a>';
                $html.='<a href="javascript:'.$fun.'('.$pageCount.')">末页</a>';
            }
            else
            {
                $html.='<a href="javascript:'.$fun.'('.$pageCount.')">下一页</a>';
                $html.='<a href="javascript:'.$fun.'('.$pageCount.')">末页</a>';
            }
            return $html;
        }
        return '';
    }

}

// ------------------------------------------------------------------------
/**
 *  创建文件夹
 *
 * @access	public
 * @return	integer
 */
if (!function_exists('create_dir')) {

    function create_dir($typedir) {
        if (!is_dir($typedir)) {
            mkdir($typedir, 0777);
        }
    }
}

// ------------------------------------------------------------------------
/**
 *  去空格 包括UTF-8的全角空格
 *
 * @access	public
 * @return	public
 */
if (!function_exists('cntrim')) {

    function cntrim($s) {
        $s = mb_ereg_replace('^(([ \r\n\t])*(　)*)*', '', $s);
        $s = mb_ereg_replace('(([ \r\n\t])*(　)*)*$', '', $s);
        return $s;
    }

}

// ------------------------------------------------------------------------
/**
 *截取字符串 涵数
 *$str  字符串
 *$start  开始位置
 *$len  截取长度
 */
if (!function_exists('msubstr')) {
    function msubstr($str, $start, $len) {
        $tmpstr = "";
        $strlen = $start + $len;
        for($i = 0; $i < $strlen; $i++) {
            if(ord(substr($str, $i, 1)) > 0xa0) {
                $tmpstr .= substr($str, $i, 2);
                $i++;
            } else
                $tmpstr .= substr($str, $i, 1);
        }
        return $tmpstr;
    }
}

// ------------------------------------------------------------------------
/**
 * 将数组键值替换
 *  author:yuchong
 * @access	public
 * @return	integer
 */
if ( ! function_exists('array_change_key'))
{
    function array_change_key($arr,$key='id'){
        $return = array();
        if(is_array($arr) && !empty($arr)){
            foreach($arr as $val){
                $return[$val[$key]] = $val;
            }
        }
        return $return;
    }
}

/**
 *   编码转换
 *   author:yuchong
 */
if( !function_exists('str_charset')){
    function str_charset($in_charset, $out_charset, $str_or_arr)
    {
        $lang = array(&$in_charset, &$out_charset);
        foreach ($lang as &$l)
        {
            switch (strtolower(substr($l, 0, 2)))
            {
                case 'gb': $l = 'gbk';
                    break;
                case 'bi': $l = 'big5';
                    break;
                case 'ut': $l = 'utf-8';
                    break;
            }
        }
        if(is_array($str_or_arr))
        {
            foreach($str_or_arr as &$v)
            {
                $v = str_charset($in_charset, $out_charset, $v);
            }
        }
        else
        {
            $str_or_arr = iconv($in_charset, $out_charset, $str_or_arr);
        }
        return $str_or_arr;
    }
}




/**
 *   author:yuchong
 *   字符串切割函数
 */
if(!function_exists('str_cutword')){
    function str_cutword($string,$length=80,$charset="utf-8",$etc='...')
    {
        $start = 0;
        if (!$length) return $string;
        if (function_exists ("mb_substr" ))
            return mb_substr ( $string, $start, $length, $charset );
        $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all ( $re [$charset], $string, $match );
        $slice = join ( "", array_slice ( $match [0], $start, $length ) );

        return $slice.$etc;
    }
}

/**
 *  时间格式化函数
 *  author：yuchong
**/
if(!function_exists('time_format')){
    function time_format($time, $format = 'Y年n月j日 G:i:s', $second = false)
    {
        $diff = time() - $time;
        if ($diff < 60 && $second)
        {
            return $diff.'秒前';
        }
        $diff = ceil($diff/60);
        if ($diff < 60)
        {
            return $diff.'分钟前';
        }
        $d = date('Y,n,j', time());
        list($year, $month, $day) = explode(',', $d);
        $today = mktime(0, 0, 0, $month, $day, $year);
        $diff = ($time-$today) / 86400;
        switch (true)
        {
            case $diff < -2:
                break;
            case $diff < -1:
                $format = '前天 '.($second ? 'G:i:s' : 'G:i');
                break;
            case $diff < 0:
                $format = '昨天 '.($second ? 'G:i:s' : 'G:i');
                break;
            default:
                $format = '今天 '.($second ? 'G:i:s' : 'G:i');
        }
        return date($format, $time);
    }
}

